/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

#pragma once

#include <QMainWindow>
#include <QProgressBar>
#include <QCheckBox>
#include <QLabel>

#include "ui_auto-mate.h"

#include "installer.h"
#include "appsmodel.h"
#include "logwatcher.h"
#include "configurationui.h"
#include "searchlineedit.h"

/**
	Main application GUI.
*/
class AutoMateUi : public QMainWindow, private Ui::AutoMate {
	Q_OBJECT

	private:
		/** Controllers **/
		TailWatch *logWatcher; /// Log file watcher.
		AppsModel *amodel;

		/** Gui elements **/
		SearchLineEdit *searchEdit;
		QProgressBar *downloadProgressBar;
		QCheckBox *download_checkBox;
		QLabel *progressName;
		ConfigurationUi *config;

		std::string last_database_location; /// FIXME This is a hack to reload database on location change.

		/**
			Connect actions.
		*/
		void actionsConnect();

		/**
			Setup UI.
		*/
		void setupUi(QMainWindow *widget);

		/**
			Connect UI.
		*/
		void connectUi();

	public slots:

		void updateDataReadProgress(int bytesRead, int totalBytes);
		void resetApplyAction();
		void updateStatusMessage(QString message);
		void updateProgressName(QString name);

	public:
		/**
			Default constructor.
			@param parent Parent for Qt heirarchy and memory management.
		*/
		AutoMateUi(QWidget* parent = 0);

		void setDownloadOnly(bool state) {
			download_checkBox->setChecked(state);
		}

		void loadTemplate(std::string filename);
		void applySelected();

	protected slots:
		/**
			Exits the application.
		*/
		void quit();

		/**
			Pops up "About" dialog.
		*/
		void about();

		/**
			Pops Export.. dialog and saves to hard-drive.
		*/
		void save();

		/**
			Pops Import.. dialog and updates selection.
		*/
		void load();

		/**
			Clears selection on applications view.
		*/
		void clearSelection();

		/**
			Applies the selected tweaks from the tweaks view.
		*/
		void tweak();

		/**
			Apply selection (download and/or install applications or apply tweaks)
		*/
		void apply();

		/**
			Filters the views by searchEdit's value.
		*/
		void filterViews();

		/**
			Forces reload of models.
		*/
		void reloadModels();

		/**
			Reload models if database location changed.

			FIXME ugly hack.
		*/
		void reloadModelsOnDatabaseChange();

		void hideHelp();
		
		void showContents();

		void showQuickHelp();

#ifdef Q_OS_WIN32
		void requestReboot();
		void setUACState(bool state);
#endif

	signals:
		void apply(std::vector<Version> versions, bool download_only);
		void abort();
		void start();
};
